package renpyas3.view.instructions
{
	import flash.events.Event;
	import org.osflash.thunderbolt.Logger;
	import renpyas3.model.RenpyPlayerModel;
	import renpyas3.view.components.Hud;
	import renpyas3.view.RenpyPlayer;
	
	/**
	 * text for the narrator (not assigned to any character)
	 */
	public class ViewInstruction_textFromNarrator extends ViewInstruction
	{
		public static const NAME:String = "textFromNarrator";

		private var _view:RenpyPlayer;

		public function ViewInstruction_textFromNarrator() 
		{
			
		}
		
		/**
		 * execute a the code for this command, altering RenpyPlayerViewState
		 * @param	view  is the whole set of view state variables and operations that make up the VIEW
		 * @param	model the data needed to play a renpy novel
		 * @param	params   extra data for some instructions
		 */
		override public function execute(view:RenpyPlayer, model:RenpyPlayerModel, params:Object=null):void
		{
			_view = view;

			//view.hud.text = String(params);
			view.hud.animateTextWriting(String(params));
			view.hud.addEventListener(Hud.FINISHED_ANIMATING_TEXT_WRITING, handleTextWritingAnimFinished, false, 0, true);
			Logger.info("ViewInstruction_textFromNarrator::execute  --textFromNarrator:"+params);
		}		

		/**
		 * finish immediately, FastForward writing anim
		 */
		override public function abort():void
		{
			_view.hud.abortTextWritingAnim();
			handleTextWritingAnimFinished();
		}
		
		private function handleTextWritingAnimFinished(e:Event=null):void 
		{
			_view.hud.removeEventListener(Hud.FINISHED_ANIMATING_TEXT_WRITING, handleTextWritingAnimFinished);

			//all done in this command
			_view.instructionsProcessor.instructionFinished();	
			
			//clean references
			_view = null;
		}
		
	}

}